package io.plus.server.command.command_handler.auth;

import io.plus.common.packet.auth.HeartbeatRequestMessage;
import io.plus.common.packet.auth.HeartbeatResponseMessage;
import io.plus.common.packet.metadata.Command;
import io.plus.common.packet.metadata.ResponseMessage;
import io.plus.common.protocol.Packet;
import io.plus.engine.pulse.AsynchronousSocketChannelContext;
import io.plus.server.Status;
import io.plus.server.UserConfiguration;
import io.plus.server.command.CommandHandler;
import io.plus.server.utils.ImKit;
import io.plus.server.utils.JsonKit;

public class HeartbeatCommandHandler implements CommandHandler {
    @Override
    public Command command() {
        return Command.heartbeat_request;
    }

    @Override
    public Packet handler(UserConfiguration userConfiguration, AsynchronousSocketChannelContext asynchronousSocketChannelContext, Packet packet) throws Exception {
        HeartbeatRequestMessage heartbeatRequestMessage = JsonKit.toBean(packet.getBody(), HeartbeatRequestMessage.class);
        String token = heartbeatRequestMessage.getToken();
        //
        boolean ok = true;
        if (ok) {
            ResponseMessage responseMessage = new ResponseMessage(Command.heartbeat_response).setData(new HeartbeatResponseMessage(Command.heartbeat_response, Status.heartbeat_succeed));
            Packet heartbeatPacket = ImKit.convertResponsepPacket(asynchronousSocketChannelContext, responseMessage);
            return heartbeatPacket;
        } else {
            ResponseMessage responseMessage = new ResponseMessage(Command.heartbeat_response).setData(new HeartbeatResponseMessage(Command.heartbeat_response, Status.heartbeat_failed));
            Packet heartbeatPacket = ImKit.convertResponsepPacket(asynchronousSocketChannelContext, responseMessage);
            return heartbeatPacket;
        }

    }
}
